package com.pl.service.impl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.pl.bean.ManageUserBean;
import com.pl.common.Constant;
import com.pl.common.Constant.UserJobStatus;
import com.pl.common.Constant.sessionKey;
import com.pl.dao.ManageUserBeanDao;
import com.pl.service.ManageUserService;
import com.pl.util.PasswordUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.List;

/**
 * 
 */
@Service("manageUserService")
public class ManageUserServiceImpl implements ManageUserService{

	private static Logger log = LogManager.getLogger();

	@Resource(name="manageUserBeanDao")
	private ManageUserBeanDao manageUserBeanDao;

	/**
	 * 新增
	 */
	@Transactional
	public void addManageUser(ManageUserBean manageUserBean){
		
		manageUserBeanDao.addManageUserBean(manageUserBean);
	}

	/**
	 * 修改
	 */
	@Transactional
	public void updateManageUser(ManageUserBean manageUserBean){
		
		manageUserBeanDao.updateManageUserBean(manageUserBean);
	}
	
	/**
	 * 根据条件获得数量
	 */
	public int getManageUserNum(ManageUserBean manageUserBean){
		
		return manageUserBeanDao.getManageUserBeanNum(manageUserBean);
	}
	
	/**
	 * 根据条件获得数据
	 */
	public List<ManageUserBean> getManageUser(ManageUserBean manageUserBean){
		
		return manageUserBeanDao.getManageUserBean(manageUserBean);
	}
	
	/**
	 * 根据条件获得数据
	 */
	public ManageUserBean getManageUserById(Long id){
		
		ManageUserBean manageUserBean = new ManageUserBean();
		manageUserBean.setId(id);
		
		List<ManageUserBean> list = getManageUser(manageUserBean);
		if(list !=null && !list.isEmpty()){
    		return list.get(0);
    	}
    	
    	return null;
	}
	
	/**
	 * 根据条件获得数据
	 */
	public ManageUserBean getManageUserByUserName(String userName){
		
		ManageUserBean manageUserBean = new ManageUserBean();
		manageUserBean.setUser_name(userName);
		
		List<ManageUserBean> list = getManageUser(manageUserBean);
		if(list !=null && !list.isEmpty()){
    		return list.get(0);
    	}
    	
    	return null;
	}

	@Override
	public String validateLogin(HttpServletRequest request, String userName, String password, String imageCode) {
		
//		if(Strings.isNullOrEmpty(imageCode)) {
//			return "图形验证码不可为空";
//		}
//		
//		String sessionImageCode = (String) request.getSession().getAttribute(sessionKey.LOGIN_IMAGE_CODE.getValue());
//		if(!Objects.equal(imageCode, sessionImageCode)) {
//			return "图形验证码错误";
//		}
		
		if(!Strings.isNullOrEmpty(userName) && !Strings.isNullOrEmpty(password)){
			
			ManageUserBean userBean = this.getManageUserByUserName(userName);
			
			if(userBean == null){
				return "不存在此用户";
			}else{
				/*
				 * if(!Objects.equal(userBean.getJob_status(), UserJobStatus.on.name())) {
				 * //不是在职状态 return "你已离职，请联系管理员"; }
				 */
				
				if(PasswordUtil.matches(userBean.getPassword(), password)){
					return null;
				}else{
					return "密码不正确";
				}
			}
		}
		return "不存在此用户";
	}
	
	@Override
	public String validateLogin(String userName, String password, String imageCode) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return this.validateLogin(request, userName, password, imageCode);
	}

	@Override
	public void afterSuccessLogin(HttpServletRequest request, ManageUserBean userBean) {
		
		//登陆成功
		HttpSession seesion = request.getSession();
		seesion.setAttribute(sessionKey.OAUTH_USER_ID.getValue(), "oauth_" + userBean.getId());
		seesion.setAttribute(sessionKey.USER_ID.getValue(), userBean.getId());
		seesion.setAttribute(sessionKey.REAL_NAME.getValue(), userBean.getReal_name());
	}

	@Override
	public ManageUserBean getManageUser(Authentication authentication) {
		
		String username = authentication.getName();
    	if(authentication.getName().startsWith(Constant.AuthenticationNamePRE)) {
    		username = username.replace(Constant.AuthenticationNamePRE, "");
    	}
    	
    	ManageUserBean userBean = this.getManageUserByUserName(username);
    	return userBean;
	}
}
